using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Web;
using System.Web.Mail;
using System.Web.Services;
using System.Xml;
using Dimok.Aspx.Manager.Lib;
using Dimok.Exceptions;
using Dimok;
using Dimok.Data;
using Dimok.Config;
using Dimok.User;
using V = Dimok.Verification;
using Data;

namespace Dimok.Aspx.Manager.Underwriting.WS {
	public class Decision : Dimok.Aspx.Manager.Lib.WS {
		public Decision() {
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
		}

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;
				
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if(disposing && components != null) {
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		#region Conditions
		[WebMethod]
		public string[] DeleteCondition(int verID){
			try{
				if( verID > 0 )V.WS.UpdateChecked(verID,true);
				else V.WS.DeleteVerItem(verID);
				return ConditionsAll();
			}catch(Exception exc){throw new ClientException(exc);}
		}
		[WebMethod]
		public string[] AddCondition(int verID,string lable){
			try{
				if( verID != 0 )V.WS.UpdateChecked(verID,false);
				else{
					DataSet ds = new DataSet();
					ds.Tables.Add(SQL.LoadTable(new HashTableEx("Lable",lable,"IsCustom",1,"ParentID",26)));
					V.WS.AddItemInfo(ds);
				}
				return ConditionsAll();
			}catch(Exception exc){throw new ClientException(exc);}
		}
		[WebMethod]
		public void UpdateCustomCondition(int verID,string lable){
			try{
				DataSet ds = new DataSet();
				ds.Tables.Add(SQL.LoadTable(new HashTableEx("ID",verID,"Lable",lable,"IsCustom",1)));
				V.WS.AddItemInfo(ds);
			}catch(Exception exc){throw new ClientException(exc);}
		}
		[WebMethod]
		public string[] ConditionsAll(){
			try{return new string[]{uox.CallBackConditions.GetXml(),uox.CallBackConditionList.GetXml()};
			}catch(Exception exc){throw new ClientException(exc);}
		}
		#endregion

		[WebMethod]
		public void FaxApproval( string html ){
			try{
				uox.FaxForm(html,uox.LoanInfo.DealerFax,uox.LoanInfo.DealerName,"Callback");
				SQL.ExecuteNonQuery(db,"sh_ArchiveRow",uox.applID,"v_FaxToDealer");
			}catch(Exception exc){throw new ClientException(exc);}
		}

		[WebMethod]
		public void FaxCallback_(){
			try{
				string src = "http://air_:8080/Manager/Underwriting/Callback.aspx?ApplID="+uox.applID+"&NoLock=1&IsFax=1";
				XmlDocument doc = new XmlDocument();
				XmlNode root = doc.AppendChild(doc.CreateElement("URLS"));
				root.AppendChild(doc.CreateElement("URL")).AppendChild(doc.CreateTextNode(src));
				MailMessage m = new MailMessage();
				m.BodyFormat = MailFormat.Text;
				m.From = uox.UserName;
				m.To = application.FaxEMail;
				m.Subject = uox.LoanInfo.DealerName+"[fax:"+uox.LoanInfo.DealerFax+"]";
				m.Body = doc.InnerXml;
				SmtpMail.Send(m);
			}catch(Exception exc){ throw new ClientException(exc); }
		}
		[WebMethod]
		public void FaxCallback(){
			try{
				uox.FaxCallBack();
			}catch(Exception exc){ throw new ClientException(exc); }
		}
		[WebMethod]
		public void XmlCreditInfo(string fieldName,string fieldValue){
			try{
				uox.CreditInfo.Update(fieldName,fieldValue);
			}catch(Exception exc){ throw new ClientException(exc); }
		}
		[WebMethod]
		public string xmlLoanInfoTIL(string fieldName,string fieldValue){
			return XmlLoanInfo(fieldName,fieldValue);
		}
		[WebMethod]
		public string xmlLoanInfo(string fieldName,string fieldValue){
			return XmlLoanInfo(fieldName,fieldValue);
		}
	}
}
